<?php

Raise::load('core.RaiseConvert');

/**
 * TestCoreRaiseConvert class
 * Testing unit of 'phpraise/core/RaiseConvert.php'
 *
 * @author Sam-Mauris Yong <hellclanner at live dot com>
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package Raise.UnitTest.Test
 */
class TestCoreRaiseConvert extends RaiseTest {

    public static function testClass() {
        self::assertClassExists('RaiseConvert');
        self::assertClassExtends('RaiseConvert', 'RaiseStaticClass');
        self::assertClassMethod('RaiseConvert', array(
            'toString',
            'toInteger',
            'toBoolean',
            'toDouble',
            'toArray',
            'toObject',
            'toDateTime',
            'toJSONString',
            'fromJSONString',
            'toPoint',
            'toRectangle',
            'binaryToDecimal',
            'decimalToBinary',
            'binaryToHexadecimal',
            'hexadecimalToBinary',
            'hexadecimalToDecimal',
            'decimalToHexadecimal',
            'decimalToOctal',
            'octalToDecimal',
            'degreeToRadian',
            'radianToDegree',
            'fromBase64',
            'toBase64',
            'arbitraryBase'
        ));
    }

    public static function testToString() {
        self::assertStrictEqual(RaiseConvert::toString('hi'), 'hi');
        self::assertStrictEqual(RaiseConvert::toString(5), '5');
        self::assertStrictEqual(RaiseConvert::toString(5.6), '5.6');
    }

    public static function testToBoolean() {
        self::assertStrictEqual(RaiseConvert::toBoolean('hi'), true);
        self::assertStrictEqual(RaiseConvert::toBoolean(''), false);
        self::assertStrictEqual(RaiseConvert::toBoolean(0), false);
        self::assertStrictEqual(RaiseConvert::toBoolean(null), false);
        self::assertStrictEqual(RaiseConvert::toBoolean(5), true);
    }

    public static function testToInteger() {
        self::assertStrictEqual(RaiseConvert::toInteger('hi'), 0);
        self::assertStrictEqual(RaiseConvert::toInteger('6hi'), 6);
        self::assertStrictEqual(RaiseConvert::toInteger(5), 5);
        self::assertStrictEqual(RaiseConvert::toInteger(5.6), 5);
    }

    public static function testToDouble() {
        self::assertStrictEqual(RaiseConvert::toDouble('5.7'), 5.7);
        self::assertStrictEqual(RaiseConvert::toDouble('6'), 6.0);
        self::assertStrictEqual(RaiseConvert::toDouble(5), 5.0);
    }

    public static function testToArray() {
        $c = new stdClass();
        $c->test = 50;
        self::assertStrictEqual(RaiseConvert::toArray($c), array('test' => 50));
    }

    public static function testToObject() {
        $c = new stdClass();
        $c->test = 50;
        self::assertEqual(RaiseConvert::toObject(array('test' => 50))->test, $c->test);
        self::assertEqual(RaiseConvert::toObject(array('test' => 50)), $c);
    }

    public static function testToDateTime() {
        self::assertObjectClass(RaiseConvert::toDateTime('Thu, 21 Dec 2000 16:01:07 +0200'), 'RaiseDateTime');
        self::assertStrictEqual(RaiseConvert::toDateTime('Thu, 21 Dec 2000 16:01:07 +0200')->year(), 2000);
        self::assertObjectClass(RaiseConvert::toDateTime(1306479751), 'RaiseDateTime');
        self::assertStrictEqual(RaiseConvert::toDateTime(1306479751)->year(), 2011);
        self::assertStrictEqual(RaiseConvert::toDateTime(1306479751)->month(), 5);
        self::assertObjectClass(RaiseConvert::toDateTime(null), 'RaiseDateTime');
        self::assertStrictEqual(RaiseConvert::toDateTime(null)->year(), RaiseDateTime::now()->year());
        self::assertStrictEqual(RaiseConvert::toDateTime(null)->month(), RaiseDateTime::now()->month());
    }

    public static function testToJSONString() {
        $a = array('test' => 50);
        self::assertStrictEqual(RaiseConvert::toJSONString($a), '{"test":50}');
        self::assertStrictEqual(RaiseConvert::toJSONString(array()), '[]');
    }

    public static function testFromJSONString() {
        $a = array('test' => 50);
        self::assertStrictEqual(RaiseConvert::fromJSONString('{"test":50}')->toArray(), $a);
        self::assertStrictEqual(RaiseConvert::fromJSONString('[]')->toArray(), array());
    }

    public static function testToPoint() {
        $p = new RaisePoint(50, 20);
        self::assertObjectClass(RaiseConvert::toPoint(new Rectangle($p, 100, 20)), 'RaisePoint');
        self::assertStrictEqual(RaiseConvert::toPoint(new Rectangle($p, 100, 20)), $p);
        self::assertStrictEqual(RaiseConvert::toPoint(new RaiseCollection(50, 20))->x(), $p->x());
        self::assertStrictEqual(RaiseConvert::toPoint(new RaiseCollection(50, 20))->y(), $p->y());
        self::assertStrictEqual(RaiseConvert::toPoint('50, 20')->x(), (double) $p->x());
        self::assertStrictEqual(RaiseConvert::toPoint('50,20')->y(), (double) $p->y());
    }

    public static function testToRectangle() {
        self::assertObjectClass(RaiseConvert::toRectangle('20,30,50,10'), 'Rectangle');
        self::assertStrictEqual(RaiseConvert::toRectangle('20,30,50,10')->x(), 20.0);
        self::assertStrictEqual(RaiseConvert::toRectangle('20,30,50,10')->y(), 30.0);
        self::assertStrictEqual(RaiseConvert::toRectangle('20,30,50,10')->x2(), 70.0);
        self::assertStrictEqual(RaiseConvert::toRectangle('20,30,50,10')->y2(), 40.0);
        self::assertStrictEqual(RaiseConvert::toRectangle(array(20, 30, 50, 10))->x(), 20.0);
    }

    public static function testBaseConverts(){
        self::assertStrictEqual(RaiseConvert::binaryToDecimal('10010'), 18);
        self::assertStrictEqual(RaiseConvert::decimalToBinary(18), '10010');
        self::assertStrictEqual(RaiseConvert::binaryToHexadecimal('10010'), '12');
        self::assertStrictEqual(RaiseConvert::hexadecimalToBinary('12'), '10010');
        self::assertStrictEqual(RaiseConvert::hexadecimalToDecimal('12'), 18);
        self::assertStrictEqual(RaiseConvert::decimalToHexadecimal('12'), 'c');
        self::assertStrictEqual(RaiseConvert::decimalToOctal('1024'), '2000');
        self::assertStrictEqual(RaiseConvert::octalToDecimal('2000'), 1024);

        $m = 'some r4nd0m t3xt';
        self::assertStrictEqual(RaiseConvert::toBase64($m), base64_encode($m));
        self::assertStrictEqual(RaiseConvert::fromBase64(RaiseConvert::toBase64($m)), $m);

        $i = 35.0;
        self::assertStrictEqual(RaiseConvert::degreeToRadian($i), deg2rad($i));
        self::assertStrictEqual(RaiseConvert::radianToDegree(RaiseConvert::degreeToRadian($i)), $i);

        self::assertStrictEqual(RaiseConvert::arbitraryBase(45, 10, 16), RaiseConvert::decimalToHexadecimal(45));
        self::assertStrictEqual((int)RaiseConvert::arbitraryBase('1001', 2, 10), RaiseConvert::binaryToDecimal('1001'));
    }

}